package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.BrandCategoryMapper;
import cn.tedu.csmall.product.mapper.BrandMapper;
import cn.tedu.csmall.product.pojo.dto.BrandAndNewDTO;
import cn.tedu.csmall.product.pojo.entity.Brand;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.repsitory.IBrandRedisRepository;
import cn.tedu.csmall.product.service.IBrandService;
import cn.tedu.csmall.product.web.JsonResult;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class BrandServiceImpl implements IBrandService, ServiceCode {
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private  BrandCategoryMapper brandCategoryMapper;
    @Autowired
    private IBrandRedisRepository repository;

    public BrandServiceImpl(){
        log.info("创建业务类：BrandServiceImpl");
    }

    @Override
    public void andNew(BrandAndNewDTO brandAndNewDTO) {
        log.debug("开始处理添加平牌业务: BrandServiceImpl ");
        String name=brandAndNewDTO.getName();
        int countByName=brandMapper.countByName(name);
        log.debug("开始添加品牌，品牌名称：{} 平牌数量：{}",name,countByName);
        if(countByName>0){
            String message="添加品牌失败! 品牌名:【"+name+"】已存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Brand brand=new Brand();
        LocalDateTime now= LocalDateTime.now();
        BeanUtils.copyProperties(brandAndNewDTO,brand);
        brand.setGmtCreate(now);
        brand.setGmtModified(now);
        log.debug("即将插入数据:{}",brand);
       int row=brandMapper.insert(brand);
       if(row!=1){
           String message="添加品牌失败！服务器忙，请稍后再一次尝试!";
           log.debug(message);
           throw new ServiceException(ServiceCode.ERR_INSERT,message);
       }
    }
    @Override
    public void delete(Long id) {
        log.debug("开始处理删除品牌功能!参数：{}",id);
        BrandStandardVO standardVO = brandMapper.getStandardVO(id);
        log.debug("开始尝试删除品牌数据id:{},删除品牌数据",id,standardVO);
        if (standardVO==null) {
            String message="删除数据是失败!品牌"+id+"数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int brandCategory = brandCategoryMapper.countByBrandCategory(id);
        if (brandCategory>1){
          throw new ServiceException(ServiceCode.ERR_DELETE,"品牌已经关联了类别，不允许删除");
        }

        int byId = brandMapper.deleteById(id);
        if (byId!=1){
            String message="删除品牌失败！服务器忙，请稍后再一次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }
    @Override
    public List<BrandListItemVO> listBrand() {
        log.debug("开始查询品牌列表");
        //
        List <BrandListItemVO>brand=repository.getList();
        if (brand.size()>0){
            return brand;
        }
        List<BrandListItemVO> list = brandMapper.listBrand();
        repository.putList(list);
        return list;
    }
    @Override
    public BrandStandardVO getBrand(Long id) {
        log.debug("开始查询品牌列表");
        //根据id从Redis中获取数据
        BrandStandardVO brand = repository.get(id);
        //判断获得结果是否为null
        if (brand != null){
            //是直接返回
            return brand;
        }
        //Redis中无此数据，调用Mapper查询
        BrandStandardVO standardVO = brandMapper.getStandardVO(id);
        //判断查询结果是否为null
        if (standardVO == null) {
            //是抛出异常
            String msseage="你查询的品牌表格数据不存在";
            log.debug(msseage);
            //否：将查询结果存入Redis，并还回结果
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,msseage);
        }else {
            repository.put(standardVO,1, TimeUnit.MINUTES);
            return standardVO;
        }
    }

    @Override
    public void lodBrandToCache(){
        //调用RedisRepository删除缓存中的品牌列表
        Set<String> Keys = repository.getAllKeys();
       repository.deleteAll(Keys);
        //调用Mapper查询品牌列表
        List<BrandListItemVO> list = brandMapper.listBrand();
        //调用RedisRepository将品牌列表写入到缓存中
        repository.putList(list);
        for (BrandListItemVO brand:list) {
            BrandStandardVO brandStandardVO=brandMapper.getStandardVO(brand.getId());
            repository.put(brandStandardVO);
        }
        //
        //以上Mapper查询结果包含所有品牌的数据，每个数据中都有id
        //
    }
}
